import os
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
# 头像
class AvatarLabel(QLabel):
    def __init__(self, size=100, parent=None):
        super().__init__(parent)
        self.size = size
        self.setFixedSize(size, size)
        self.setStyleSheet('background-color:#ffffff;border:1px solid #dddddd;border-radius:6px;')
        self.setScaledContents(True)
        self._path = None

    def set_avatar(self, path):
        if path and os.path.exists(path):
            pix = QPixmap(path).scaled(self.size, self.size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            self.setPixmap(pix)
            self._path = path
        else:
            self.setPixmap(QPixmap())
            self._path = None

    def avatar_path(self):
        return self._path
